<?php

namespace App\Http\Controllers\Admin;

use App\Http\Query\CategoryQuery;
use app\Model\Base;
use app\Model\OrderModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Log;
use function MongoDB\BSON\toJSON;
use SplFileObject;

class JapanShowStockController extends Controller

{

    function show_stock(Request $request)
    {
        $getParam = $request->all();
        $map = [];
        if (!empty($getParam['storehouse'])) {
            $map['h.storehouse_id'] = $getParam['storehouse'];
        }
        if (!empty($getParam['category'])) {

            $map['m.category_id'] = $getParam['category'];
        }
        if (!empty($getParam['goods_code_code'])) {
            $map['g.goods_code'] = $getParam['goods_code_code'];
        } else {
            if (!empty($getParam['goods_code_name'])) {
                $map['g.goods_code'] = $getParam['goods_code_name'];
            }
        }
        if (!empty($getParam['color'])) {
            $map['sds.color'] = $getParam['color'];
        }
        if (!empty($getParam['size'])) {
            $map['sds.size'] = $getParam['size'];
        }

        $list = DB::table('storehouse_detailed as h')
            ->select(DB::raw('
                                m.category_name,
                                m.category_id,
                                sds.color,
                                sds.size,
                                sds.model_id,
                                sds.num,
                                sds.bad_num,
                                sds.web_num,
                                g.goods_name,
                                g.goods_code,
                                h.detailed_code,
                                h.stock_num,
                                h.storehouse_id'))
            ->leftJoin('goods_master as g', 'g.goods_id', '=', 'h.goods_id')
            ->leftJoin('category_master as m', 'm.category_id', '=', 'g.category_id')
            ->leftJoin('storehouse_detailed_size as sds', 'sds.goods_id', '=', 'h.goods_id')
            ->where($map)
            ->get();


        $mo = new \App\Model\Base();
        return view('warehouse.show_stock', array(
            'storehouse' => $mo::storehouse(),
            'category' => $mo::category(),
            'goods' => $mo::goods(),
            'colors' => $mo::colors(),
            'size' => $mo::size(),
            'list' => $list,
            'param' => $getParam,
        )); // 返回视图

    }
}
